package com.hdu.dwh.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

import com.hdu.dwh.service.flink_service.FlinkWebRestService;

@Configuration
public class FlinkRestConfig {

    @Value("${dwh.flink.rest.base-url}")
    private String flinkRestBaseUrl;

    @Value("${dwh.flink.rest.username}")
    private String username;

    @Value("${dwh.flink.rest.password}")
    private String password;
    
    @Bean
    public FlinkWebRestService flinkWebService() {
        RestClient restClient = RestClient.builder()
                .baseUrl(flinkRestBaseUrl)
                .defaultHeaders(headers -> headers.setBasicAuth(username, password))
                .build();
        RestClientAdapter adapter = RestClientAdapter.create(restClient);
        HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter)
                .build();
        return factory.createClient(FlinkWebRestService.class);
    }
}
